package async.complete;


import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

public class Demo01 {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(100);
        System.out.println(Thread.currentThread().getName());
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("异步任务线程：" + Thread.currentThread().getName());
                TimeUnit.SECONDS.sleep(3);
                System.out.println("child run");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 123;
        }, executor);
        System.out.println("main end");
        try {
            future.whenCompleteAsync(new BiConsumer<Integer, Throwable>() {
                @Override
                public void accept(Integer integer, Throwable throwable) {
                    System.out.println("结果触发任务线程：" + Thread.currentThread().getName());
                    System.out.println("特定任务执行");
                    // 放在在这里执行
                    executor.shutdown();
                }
            },executor);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
